package cn.yqx.bankshop.oauth2.authentication.code;

import cn.hutool.extra.spring.SpringUtil;
import cn.yqx.bankshop.constants.HttpConstant;

import cn.yqx.bankshop.oauth2.handle.CustomizeAuthenticationFailureHandler;
import cn.yqx.bankshop.oauth2.store.VcodeManager;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.filter.OncePerRequestFilter;


import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Slf4j
@Data
@NoArgsConstructor
@AllArgsConstructor
@Component
public class ImageCodeValidateFilter extends OncePerRequestFilter {

    private static final String AUTH_IMG_KEY = "authImgKey" ;
    private static final String IMAGE_CODE = "imageCode" ;

    private CustomizeAuthenticationFailureHandler customizeAuthenticationFailureHandler = new CustomizeAuthenticationFailureHandler(); //验证码失败的处理器

    @Autowired
    VcodeManager vcodeManager ;
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {

        if(HttpConstant.LOGIN_URL.equals(request.getRequestURI()) && request.getMethod().equalsIgnoreCase(HttpConstant.POST)){
            try{
                //验证码验证码
                redisValidate(request);
            }
            catch (ValidateImageCodeExpireException e){
                customizeAuthenticationFailureHandler.onAuthenticationFailure(request, response, e);
                e.printStackTrace();
                return;
            }
            catch (ValidateImageCodeException e){
                customizeAuthenticationFailureHandler.onAuthenticationFailure(request, response, e);
                e.printStackTrace();
                return;
            }
        }
        filterChain.doFilter(request, response);
    }
    private void redisValidate(HttpServletRequest request) throws ServletRequestBindingException {

        String authImgKey = ServletRequestUtils.getStringParameter(request,AUTH_IMG_KEY) ;
        String imageCode = ServletRequestUtils.getStringParameter(request,IMAGE_CODE) ;

        if (StringUtils.isEmpty(authImgKey) || null == vcodeManager.getVcode(authImgKey)) {
            throw new ValidateImageCodeException("验证码已过期" + authImgKey);
        }

        String imageCodeInRedis = vcodeManager.getVcode(authImgKey).toString();
        if ( StringUtils.isEmpty(imageCode)  && !imageCodeInRedis.equalsIgnoreCase(imageCode)) {
            throw new ValidateImageCodeExpireException("验证码不匹配");
        }

        vcodeManager.removeVcode(authImgKey);
    }
}
